
package backupUtility;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

public class AddBackupHere  extends JFrame implements ActionListener{
    private JPanel scrollPanel;
    private JPanel buttonPanel;
    private JScrollPane scrollPane;
    private JTable choicesTable;
    private FileListGetter getList;
    private JButton saveButton;
    private JButton cancelButton;
    private JLabel chooseLabel;
    private ArrayList<File> arrayListofFiles;
    String folder = "";
    String password = "";
    ArrayList<File> files;
    
    public AddBackupHere(ArrayList<File> files, String folder, String password){
        this.files=files;
        this.folder=folder;
        this.password = password;
        getList = new FileListGetter();
        arrayListofFiles = getList.getArrayListofFiles();
//        arrayListofFiles.removeAll(files);
        
        Object [] columnNames = {"File Name", ""};
        Object [][] fileNames = new Object[arrayListofFiles.size()][2];
        for(int i = 0; i < fileNames.length; i++){
            fileNames[i][0] = arrayListofFiles.get(i).getName();
            for(int l = 0; l < files.size(); l ++){
                if(arrayListofFiles.get(i).getName().equals(files.get(l).getName())){
                    fileNames[i][1] = true;
                    break;
                }else{
                    fileNames[i][1] = false;
                }
            }
        }
       
        DefaultTableModel model = new DefaultTableModel(fileNames, columnNames);
        choicesTable = new JTable(model) {
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    default:
                        return Boolean.class;
                }
            }
        
        public boolean isCellEditable(int row, int column) {
            return column == 1;
            }
        };
        choicesTable.setPreferredScrollableViewportSize(choicesTable.getPreferredSize());
        choicesTable.getColumnModel().getColumn(0).setPreferredWidth(680);
        choicesTable.getColumnModel().getColumn(1).setPreferredWidth(20);
        
        scrollPane = new JScrollPane(choicesTable);
        scrollPane.setPreferredSize(new Dimension(700,400));
        
        saveButton = new JButton("Backup Files");
        saveButton.addActionListener(this);
        
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        
        chooseLabel = new JLabel("Add or Remove Items from the previous Backup");
      
        scrollPanel = new JPanel();
        scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
        scrollPanel.add(chooseLabel);
        scrollPanel.add(Box.createRigidArea(new Dimension(0,5)));
        scrollPanel.add(scrollPane);
        scrollPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(saveButton);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPanel.add(cancelButton);
        
        Container contentPane = getContentPane();
        contentPane.add(scrollPanel, BorderLayout.CENTER);
        contentPane.add(buttonPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
        setSize(800,500);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==saveButton){
                ArrayList<File> selectedFiles = new ArrayList<File>();
                for(int i = 0; i < choicesTable.getRowCount(); i++){
                    if(choicesTable.getModel().getValueAt(i,1) == true){
                       // System.out.println(arrayListofFiles.get(i).getName());
                        selectedFiles.add(arrayListofFiles.get(i));
                    }
                }
                
                try {
                    new BackupLogic(selectedFiles, folder, password);
                } catch (IOException ex) {
                    Logger.getLogger(UserInput.class.getName()).log(Level.SEVERE, null, ex);
                }
                BackupUtilityUI bu = new BackupUtilityUI(selectedFiles, folder, password);
                bu.setVisible(true);
                dispose();  
        }else if(ae.getSource()==cancelButton){
           BackupUtilityUI bu = new BackupUtilityUI(files, folder, password);
           bu.show();
           dispose();
        }
    
    }
}
